package com.keur.money.mvp

import android.view.View
import com.keur.money.base.BaseActivity
import com.keur.money.app.MyApp
import com.keur.money.base.utils.ImageUtil
import com.keur.money.databinding.IncludeMullaLicenseViewBinding
import com.keur.money.utils.dialog.KefuDialog
import com.keur.money.utils.dialog.TipsDialog
import com.keur.money.utils.event.KeurEventData
import com.keur.money.utils.event.KeurSubscriber
import com.keur.money.utils.helper.KeurCustomConfigsHelper
import com.keur.money.utils.helper.SharedHelper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import org.json.JSONArray
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/30 18:54
 * @Annotation：
 */
abstract class ProAct: BaseActivity(), KeurSubscriber, CoroutineScope by MainScope() {

    override fun onStart() {
        super.onStart()
        MyApp.app.publisher.register(this)
    }

    /**
     * 客服
     */
    fun showMullaKefuDialog(eventRemark: String = "",pName: String = "",pToken: String = ""){
        KefuDialog(this)
            .setMullaCancelable(true)
            .setMullaCanceledOnTouchOutside(true)
            .setMullaKefuPName(pName)
            .setMullaKefuPToken(pToken)
            .setMullaKefuRemark(eventRemark)
            .setMullaKefuCallback(object : KefuDialog.IDialogCallback{
                override fun onClick(view: View?) {
                }
            })
            .show()
    }

    /**
     * 错误提示
     */
    fun showMullaErrorTipsDialog(tips: String,eventRemark: String = ""){
        val tipsDialog = TipsDialog(this,eventRemark,object : TipsDialog.IDialogCallback{
            override fun onClick(view: View?) {

            }
        })
        tipsDialog.show()
        tipsDialog.setTips(tips)
    }

    /**
     * 获取当前用户VIP等级
     */
    fun getMullaUserVip(): Int{
        return KeurCustomConfigsHelper.getMullaUserVip()
    }

    /**
     * 获取用户当前积分数 (从签到信息中获取)
     */
    fun getMullaUserPoints(): Int{
        val signInfoString = SharedHelper.getKeurSPString(SharedHelper.KEUR_SPKEY.KEUR_SIGN_INFO)
        if (signInfoString.isNotEmpty()){
            val signInfo = JSONObject(signInfoString)
            return signInfo.optInt("currScore")
        }
        return 0
    }

    /**
     * 获取当前用户优惠券数量（从优惠券列表获取）
     */
    fun getMullaUserCouponCount(): Int{
        val dataString = SharedHelper.getKeurSPString(SharedHelper.KEUR_SPKEY.KEUR_USER_COUPON_INFO)
        if (dataString.isEmpty()){
            return 0
        }
        val couponList = JSONArray(dataString)
        return couponList.length()
    }

    /**
     * 牌照信息View
     */
    fun licenseView(): View{
        val licenseInfo = KeurCustomConfigsHelper.getMullaLicenseInfo()
        if (licenseInfo != null){
            val binding = IncludeMullaLicenseViewBinding.inflate(layoutInflater)
            ImageUtil.instance?.loadImageUrl(this,binding.imgLicenseLogo, licenseInfo.optString("logo"))
            binding.textLicenseDesc.text = licenseInfo.optString("desc")
            binding.textLicenseValue.text = licenseInfo.optString("companyName")
            return binding.root
        }
        return View(this)
    }

    /**
     * 处理订阅事件
     */
    override fun onKeurEvent(event: KeurEventData) {

    }

    override fun onDestroy() {
        super.onDestroy()
        MyApp.app.publisher.detach(this)
    }
}